package leetcode;

import java.util.Arrays;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author caifangyi
 * @date 2022/7/28
 */

/**
 * 912. 排序数组
 *
 * 给你一个整数数组nums，请你将该数组升序排列。
 *
 * 
 *
 * 示例 1：
 *
 * 输入：nums = [5,2,3,1]
 * 输出：[1,2,3,5]
 * 示例 2：
 *
 * 输入：nums = [5,1,1,2,0,0]
 * 输出：[0,0,1,1,2,5]
 * 
 *
 * 提示：
 *
 * 1 <= nums.length <= 5 * 104
 * -5 * 104 <= nums[i] <= 5 * 104
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode.cn/problems/sort-an-array
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
class Day0912 {
    public static void main(String[] args) {
        int[] arr = {5,1,1,2,0,0};
        Solution solution = new Solution();
        int[] ints = solution.sortArray(arr);
        System.out.println(Arrays.stream(ints).mapToObj(value -> String.valueOf(value)).collect(Collectors.joining(",")));
    }

    static class Solution {
        public int[] sortArray(int[] nums) {

            return sortGroup(nums, 0, nums.length - 1);
        }

        //归并排序
        int mergeNums[];
        public int[] sortGroup(int[] nums, int l, int r){
            mergeNums = new int[nums.length];
            merge(nums, l, r);
            return mergeNums;
        }

        private void merge(int[] nums, int l, int r) {
            if(l >= r){
                return;
            }

            //分组
            int mid = (l+r) >> 1;
            merge(nums, l, mid);
            merge(nums, mid+1, r);

            int temp = 0;
            //合并组
            int L = l, R = mid+1;
            while (L <= mid && R <= r){
                if(nums[L] < nums[R]){
                    mergeNums[temp++] = nums[L++];
                }else{
                    mergeNums[temp++] = nums[R++];
                }
            }

            while(L <= mid){
                mergeNums[temp++] = nums[L++];
            }
            while (R <= r){
                mergeNums[temp++] = nums[R++];
            }

            for (int i = 0; i < (r-l+1); i++) {
                nums[l+i] = mergeNums[i];
            }

        }


        //快排
        private int[] quickArray(int[] nums, int l, int r) {
            if(l < r){
                int pos = quickRandom(nums, l , r);
                quickArray(nums, l , pos-1);
                quickArray(nums, pos+1, r);
            }
            return nums;
        }

        private int quickRandom(int[] nums, int l, int r) {

            int random = new Random().nextInt(r - l + 1) + l;
            swap(nums, random, r);

            int poivt = nums[r];
            int j = l-1;
            for (int i = l; i < r; i++) {
                if(nums[i] <= poivt){
                    j++;
                    swap(nums, i, j);
                }
            }
            swap(nums, j+1 , r);
            return j+1;
        }

        private void swap(int[] nums, int i, int j) {
            if(nums[i] == nums[j]){
                return;
            }
            nums[i] = nums[i] ^ nums[j];
            nums[j] = nums[i] ^ nums[j];
            nums[i] = nums[i] ^ nums[j];
        }
    }
}
